Skip to content

Conversation

@greenhat616
Copy link

Close #493

I’m adding a cancellation token to Streamable HTTP to support graceful shutdowns. Because I’m unclear on the MCP protocol’s semantics for initiating termination, I’m currently just cutting off the body stream.

Motivation and Context

In desktop apps, we need a mechanism for the process to proactively terminate (e.g., for updates or user-initiated quits). Today, the MCP Client holds the connection open, which effectively prevents the app from shutting down; most users aren’t aware this is intentional client behavior.

How Has This Been Tested?

Breaking Changes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

@github-actions github-actions bot added T-core Core library changes T-examples Example code changes T-transport Transport layer changes labels Oct 21, 2025
@greenhat616
Copy link
Author

I checked the doc: https://modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle#shutdown

It seems that trigger a connection close is enough

@4t145 4t145 requested a review from Copilot November 2, 2025 18:40
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds cancellation token support to the StreamableHttpServer to enable graceful shutdown of SSE streams. When a server shutdown is initiated (e.g., via Ctrl+C), the cancellation token ensures that all active SSE connections are properly terminated.

  • Added cancellation_token field to StreamableHttpServerConfig
  • Updated sse_stream_response to accept and use a cancellation token to terminate streams
  • Modified the example to demonstrate graceful shutdown with cancellation token integration

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
examples/servers/src/counter_streamhttp.rs Updated imports, instantiated cancellation token, configured it in StreamableHttpServerConfig, and integrated cancellation into graceful shutdown handler
crates/rmcp/src/transport/streamable_http_server/tower.rs Added cancellation_token field to StreamableHttpServerConfig and passed child tokens to all sse_stream_response calls
crates/rmcp/src/transport/common/server_side_http.rs Updated sse_stream_response function to accept cancellation token and use it to terminate SSE streams via take_until

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions github-actions bot added the T-test Testing related changes label Nov 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-core Core library changes T-examples Example code changes T-test Testing related changes T-transport Transport layer changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gracefully shutdown is hang while a SSE connection is established

1 participant